<!-- docs - 副本/src/.vitepress/theme/components/AlterUserBlock.vue -->
<template>
  <div class="alter-user-block">
    <!-- <div class="alter-user-header">
      <span class="alter-user-icon">⚠️</span>
      <span class="alter-user-title">ALTER 语句</span>
    </div> -->
    
    <!-- Tab 切换 -->
    <div class="alter-user-tabs">
      <button 
        @click="activeTab = 'syntax'" 
        :class="['tab-btn', { active: activeTab === 'syntax' }]"
      >
        语法图
      </button>
      <button 
        @click="activeTab = 'code'" 
        :class="['tab-btn', { active: activeTab === 'code' }]"
      >
        代码
      </button>
    </div>
    
    <!-- Tab 内容 -->
    <div class="tab-content">
      <!-- 语法图 Tab -->
      <div v-if="activeTab === 'syntax'" class="syntax-content">
        <div v-if="error" style="color:red">{{ error }}</div>
        <div v-for="item in svgs" :key="item.name" style="margin-bottom:2em;">
          <div style="font-weight:bold;margin-bottom:0.5em;">{{ item.name }}</div>
          <div v-html="item.svg" style="overflow:auto;border:1px solid #eee;padding:1em;background:#fafbfc"></div>
        </div>
      </div>
      
      <!-- 代码 Tab -->
      <div v-if="activeTab === 'code'" class="code-content">
        <pre class="alter-user-content"><code>{{ content }}</code></pre>
    
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { generateSyntaxDiagrams } from './../../utils/syntaxDiagram'

// 接收从 markdown-it 插件传递的内容
const props = defineProps({
  content: {
    type: String,
    default: ''
  },
  ebnf: {
    type: String,
    default: ''
  }
});

const error = ref('')
const svgs = ref([])

const generate = () => {
  error.value = ''
  svgs.value = []
  
  // 检查内容是否包含 EBNF 语法（包含 ::=）
  if (props.content.includes('::=')) {
    try {
      // 使用 syntaxDiagram.ts 中的完整逻辑
      svgs.value = generateSyntaxDiagrams(props.content)
    } catch (e) {
      console.log(e)
      error.value = e.message
    }
  } else {
    // 普通 ALTER 语句，不生成语法图
    error.value = '这是普通的 ALTER 语句，不包含 EBNF 语法定义'
  }
}

// Tab 状态管理
const activeTab = ref('syntax'); // 默认显示语法图

// 组件挂载时触发的方法
onMounted(async () => {
  generate()
});

</script>

<style scoped>
.alter-user-block {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  padding: 1.5em 1.5em 1em 1.5em;
  margin: 2em 0;
  border: 1px solid #e5e7eb;
  transition: box-shadow 0.2s;
}
.alter-user-block:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.10);
}
.alter-user-header {
  display: flex;
  align-items: center;
  margin-bottom: 1em;
}
.alter-user-icon {
  font-size: 1.5em;
  margin-right: 0.5em;
}
.alter-user-title {
  font-weight: bold;
  font-size: 1.1em;
  color: #2c3e50;
}
.alter-user-tabs {
  display: flex;
  gap: 0.5em;
  margin-bottom: 1em;
}
.tab-btn {
  background: #f3f4f6;
  border: none;
  border-radius: 6px 6px 0 0;
  padding: 0.5em 1.5em;
  font-size: 1em;
  color: #555;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  outline: none;
  position: relative;
  top: 2px;
}
.tab-btn.active {
  background: #fff;
  color: #0078d4;
  font-weight: bold;
  border-bottom: 2px solid #0078d4;
  z-index: 1;
}
.tab-btn:not(.active):hover {
  background: #e5e7eb;
  color: #222;
}
.tab-content {
  background: #fafbfc;
  border-radius: 0 0 8px 8px;
  border: 1px solid #e5e7eb;
  border-top: none;
  padding: 1.2em 1em 1em 1em;
}
.syntax-content {
  min-height: 80px;
}
.code-content {
  min-height: 80px;
}
.alter-user-content {
  background: #23272e;
  color: #f8f8f2;
  border-radius: 6px;
  padding: 1em;
  font-size: 1em;
  overflow-x: auto;
  margin-bottom: 1em;
}
.alter-user-actions {
  display: flex;
  gap: 1em;
}
.alter-user-btn {
  background: #0078d4;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 0.4em 1.2em;
  font-size: 1em;
  cursor: pointer;
  transition: background 0.2s;
}
.alter-user-btn:hover {
  background: #005fa3;
}
.alter-user-btn.execute {
  background: #f59e42;
}
.alter-user-btn.execute:hover {
  background: #d97706;
}
@media (max-width: 600px) {
  .alter-user-block {
    padding: 1em 0.5em 0.5em 0.5em;
  }
  .tab-content {
    padding: 0.7em 0.2em 0.7em 0.2em;
  }
}
</style>